const db = require("./db");
const academySchema = new db.Schema({
  name: { type: String, default: "默认学院" },
});
const academyModel = db.model("academy", academySchema, "academy");
const adminSchema = new db.Schema({
  name: String,
  role: String,
  email: String,
  phone: String,
  truename: String,
  academy: { type: db.Schema.Types.ObjectId, ref: "academy" },
  status: {
    type: Boolean,
    default: true,
  },
  password: String,
});
const adminModel = db.model("admin", adminSchema, "admin");

const stageSchema = new db.Schema({
  name: { type: String, default: "默认阶段" },
  academy: { type: db.Schema.Types.ObjectId, ref: "academy" }, //归属哪个学院
  class: { type: db.Schema.Types.ObjectId, ref: 'clazz' },
  creator: { type: db.Schema.Types.ObjectId, ref: 'admin' },
})
const stageModel = db.model("stage", stageSchema, "stage");

// 试题表
const questionSchema = new db.Schema({
  title: { type: String, default: "默认题干" },
  cate: {
    type: Number, //1单选，2多选，3判断，4简答
    default: 1,
  },


  belong: { type: db.Schema.Types.ObjectId, ref: "stage" }, //归属哪个学院哪个阶段
  difficulty: {
    type: Number, //难度，1简单，2中等，3困难
    default: 1,
  },
  options: [

    {
      title: { type: String, default: "默认选项" },
      isRight: { type: Boolean, default: false }, //是否为正确答案
    },
  ],

  answer: {
    type: String,
    default: '默认解析'
  },
  timing: {
    type: Number,
    default: 1724918400,//默认值
  },//创建时间
  status: {
    type: Number,
    default: 0,
  }
})



const questionModel = db.model("questions", questionSchema, "questions");

const clazzSchema = new db.Schema({
  name: { type: String, default: "默认班级" },
  stage: { type: db.Schema.Types.ObjectId, ref: "stage" }, //归属哪个学院
});

const clazzModel = db.model("clazz", clazzSchema, "clazz");

//试卷表
const paperSchema = new db.Schema({
  name: { type: String, default: "默认试卷名称" },
  time: [Number],
  pass_score: { type: Number, default: 60 },
  out: { type: Number, default: 100 },
  creator: { type: db.Schema.Types.ObjectId, ref: "admin" },
  questions: [{ type: db.Schema.Types.ObjectId, ref: "questions" }],
  type: { type: Boolean, default: false }, //随机抽取0/固定题目1
  belong: { type: db.Schema.Types.ObjectId, ref: "stage" }, //归属哪个学院哪个阶段},
  class: [{ type: db.Schema.Types.ObjectId, ref: "clazz" }],
  publishTime: { type: Number, default: Date.now() },
  flag: { type: Number, default: 0 }, //审核状态
});
const paperModel = db.model("paper", paperSchema, "paper");

const dispositionSchema = new db.Schema({
  pid: { type: db.Schema.Types.ObjectId, ref: "paper" },
  radio: Object,
  checkbox: Object,
  short: Object,
  judgment: Object,
});

const dispositionModel = db.model(
  "disposition",
  dispositionSchema,
  "disposition"
);

//路由表
const routerSchema = new db.Schema({
  url: String,
  pid: {
    type: db.Types.ObjectId,
    ref: "router",
  },
  name: String,
});

const routerModel = db.model("router", routerSchema, "router");

const studentSchema = new db.Schema({
  name: String,
  tid: String,
  card: String,
  sex: Boolean,
  clazz: {
    type: db.Schema.Types.ObjectId,
    ref: "clazz",
  },
  phone: Number,
  address: String,
  ice: String,
  img: String,
});

const studentModel = db.model("student", studentSchema, "student");

let treeSchema = new db.Schema({
  name: String,
  aid: db.Types.ObjectId,
  sid: db.Types.ObjectId,
});

let treeModel = db.model("tree", treeSchema, "tree");

//考试结果表
const resultSchema = new db.Schema({
  student: {
    type: db.Schema.Types.ObjectId,
    ref: 'student'
  },
  paper: {
    type: db.Schema.Types.ObjectId,
    ref: 'paper'
  },
  result: Object,
  time: [Number],
  grade: Number,
  status: {
    type: Boolean,
    default: false
  },
  clazz: {
    type: db.Schema.Types.ObjectId,
    ref: 'clazz'
  },
})
const resultModel = db.model('result', resultSchema, 'result')
module.exports = {
  academyModel,
  stageModel,
  questionModel,
  paperModel,
  adminModel,
  routerModel,
  studentModel,
  clazzModel,
  dispositionModel,
  treeModel,
  resultModel
};

